Parsing XML Files
This topic describes how to prepare XML data and how to build a sample project to parse an XML file.
Preparing the XML Data
Before processing an XML file, remove any extraneous information from the XML file. In particular, remove any & characters and redundant lines at the start of the file, such as the version and encoding declaration, for example, <?xml version="1.0" encoding="UTF-8"?>.
Also check the consistency of the file tagging. If the file is missing a root node, add it by concatenating a root node to the start and end of the text inside your project.
You can also remove all XML namespace attributes from the entire document to enable regular work with the document. After loading the XML file, use the Remove XML Namespace function in the Xml Document library type. This strips all the xmlns attributes, for example:
<ram:ID xmlns:ram="http://www.ita.org/">103004</ram:ID> becomes
<ram:ID>103004</ram:ID>
Building an XML Parsing Project
This topic describes a sample project implementation of an XML parser. The dealersample.txt source file is a text file with source content extracted from an XML file. The file does not have a root node. The root node will be added in the project.
The file has several thousand <DEALER> entries, each with a series of tagged information including the following fields:
DLR_CODE
NAME
ADD1
ADD2
ADD3
CITY
PROVINCE
POSTAL
PHONE
FAX
For example:
<DEALER><DLR_CODE>02400</DLR_CODE><NAME>MOTOROLA TORONTO (CUST)</NAME><ADD1>150 Brunel Road</ADD1><ADD2> </ADD2><ADD3> </ADD3><CITY>Mississauga</CITY><PROVINCE>ON</PROVINCE><POSTAL>L4Z1T5</POSTAL><PHONE>4165551212</PHONE><FAX>0 </FAX></DEALER>
The source files for this project can be found here.
Creating the Basic XML Business Entities
You need to create the basic business entities required to store the XML data.
To create the basic XML business entities:
1. | In the Business Entities tab, in the Types tab, under User Types, add a Dealer type with the following text properties, ADD1, ADD2, CITY, DLR_CODE, FAX, Name, PHONE, POSTAL and PROPERTIES. |
2. | Under User Types, add an XML Parser type. |
3. | Under XML Parser, add an XML Text property, with Property Type set to Text. |
4. | Add an XML Document type, with Base type set to Xml Document. |
Creating the XML Loading Function
You need to create a function that reads the text file into the XML Text property, concatenates a <Root> tag to the beginning and a </Root> tag to the end of the XML Text, and then loads the Xml Document.
To create the XML loading function:
1. | Copy the dealersample.txt to your computer, for example, to c:\temp. |
2. | Under XML Parser, add an Init function. Do this by inserting the following instructions: |
Assign Read text from <path> dealersample.txt into XML Text
Load of Xml Document ( Concatenate [ <Root> XML Text </Root> <Add Text...> ] )
3. | Ensure that <path> is replaced with the path to the dealersample.txt file. In the sample file, this is C:\ |
Creating an XML Query Function
You need to create a function that, for each inner node (in all the child nodes of the parent node), if the name of the inner node equals a tag, returns the inner text of the inner node.
To create an XML query function:
1. | Under XML Parser, add a GetTagValue function. |
2. | Add two parameters: ParentNode with Type set to Xml Node, and Tag with Type set to Text. |
3. | Insert the following function instructions: |
For Each Xmls Node AKA innerNode In Get Child Nodes of ParentNode
{
If Name of innerNode equals (ignore case) Tag
{
Return Get InnerText of innerNode
}
Else
{
}
}
Return Not Found
Creating the XML Parsing Business Entities and Instances
You need to create various business entities that are used to parse the XML file.
To create the XML parsing business entities and instances:
1. | Under XML Parser, add a TmpNodes list, with Property Type set to List of Xml Node. |
2. | Add a RootNode type, with Base type set to Xml Node. |
3. | Add a number property, with Property Type set to Number. |
4. | Add a Nodes list, with Property Type set to List of Xml Node . |
5. | Add a Names list, with Property Type set to List of Text. |
6. | Add an instance of Dealers and XML Parser. |
Extracting the XML Data
You need to add a function that, after the required declarations, assigns the first child of the Xml document into the Xml node and assigns the child nodes of the RootNode into Nodes. It then creates the dealer business entity and populates it by using the GetTagValue function to extract the required information for each of the required nodes.
To extract the XML data:
In the Types tab, under XML Parser, add a Get Dealers function. Do this by inserting the following instructions:
Declare dealer Of Type Dealer
Declare listnodes Of Type List Of Xml Node
Declare tmpNode Of Type Xml Node
Assign Get First Child of Xml Document into RootNode
Assign [ ( Get Child Nodes of RootNode ) <Add Xml Node...> ] into Nodes
For Each Xml Node AKA node In Nodes
{
Assign Create Business Entity into dealer
Assign GetTagValue of XML Parser node, DLR CODE into DLR CODE of dealer
Assign GetTagValue of XML Parser node, NAME into Name of dealer
Assign GetTagValue of XML Parser node, ADD1 into ADD1 of dealer
Assign GetTagValue of XML Parser node, ADD2 into ADD2 of dealer
Assign GetTagValue of XML Parser node, CITY into CITY of dealer
Assign GetTagValue of XML Parser node, PHONE into PHONE of dealer
Assign GetTagValue of XML Parser node, PROVINCE into PROVINCE of dealer
Assign GetTagValue of XML Parser node, POSTAL into POSTAL of dealer
Assign GetTagValue of XML Parser node, FAX into FAX of dealer
Add dealer to Dealers
}
Creating Callouts and Testing the Project
You need to create callouts, compile the project and run monitor to test the project.
To create callouts and test the project:
1. | In the Presentation tab, in the Callout tab, add a Quick callout. |
2. | Right-click the callout and select Insert > Action Link. |
3. | Enter Init as the Action name and insert the following instruction: |
Init of XML Parser
4. | Similarly, add a callout named Get Names that is linked to Get Dealers of XML Parser. |
5. | Ensure that the Real-Time Client process (RTClient.exe) is not running. |
6. | To generate the main project, click Generate Main Project on the ribbon. Alternatively, to generate the active project, click Generate Main Project and select Generate Active Project from the drop-down list. |
7. | Verify that the project compiles without errors. |
8. | Click Run Real-Time Client. The application runs. |
9. | Click Quick. |
10. | Click Init. This runs the Init function and reads the file. |
11. | Click Get Names. This processes the file content. |
12. | Click Monitor on the ribbon. |
13. | Select Dealers. A list of the dealers extracted from the XML file appears. |